import routerName from './routerName'
import allRoutes from './allRoutes';
export interface IMenuData {
  path?: string;
  title: string;
  icon?: string;
  submenu?: null | ISubMenuData;
}

interface ISubMenuData {
  submenuTitle: string;
  belong: string;
  icon: string;
  submenuItems: IMenuData[]
}

function setMenu () {
  const NORMAL_USER: IMenuData[] = [];

  const m = JSON.parse(localStorage.getItem('routes') as string)
  
  m.forEach((i:any) => {
    // const temp = allRoutes.find((j:any) => {
    //   return j.path === i.path
    // })
    // if (!temp) return
    if (i.path === i.belongto) {
      NORMAL_USER.push({
        path: i.path,
        title: routerName[i.path],
        icon: 'icon-shouye',
        submenu: null,
      })
    } else {
      const index:any = NORMAL_USER.findIndex((k:any) => {
        return k.submenu && k.submenu.submenuTitle === routerName[i.belongto]
      })
      if (index === -1) {
        const temp:any = {
          path: '',
          name: '',
          submenu: {
            submenuTitle: routerName[i.belongto],
            icon: 'icon-shouye',
            belong: '',
            submenuItems: [
              {
                path: i.path,
                title: routerName[i.path],
                icon: 'icon-shouye',
                submenu: null
              }
            ]
          }
        }
        NORMAL_USER.push(temp)
      } else {
        const item = {
          path: i.path,
          title: routerName[i.path],
          icon: 'icon-shouye',
          submenu: null
        }
        NORMAL_USER[index].submenu?.submenuItems.push(item)
      }
    }
  })

  return NORMAL_USER
}

export { setMenu }
